package com.example.beautymakeup.controller;


import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private UploadUtil uploadToOSS;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {

        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //截取文件名后缀
        String extension = null;
        if (originalFilename != null) {
            extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        //构造新文件名称
        String objectName =  UUID.randomUUID() + extension;
        //返回文件请求路径
        String filePath = uploadToOSS.upload(file.getBytes(), objectName);
        return new Result(Code.SEND_OK,"保存成功",filePath);
    }

}

